package paylayer

import (
	"strconv"
	"strings"
	"dhfshop/models/misc"
	"encoding/json"
	"dhfshop/models/isv"
	"dhfshop/utils/math"
	//"dhfshop/domain/orders"
	"dhfshop/models/shopping"
	"github.com/astaxie/beego"
	"thirdpaysdk/api/chinaums"
	"dhfshop/service/redisgo"
	"github.com/pkg/errors"
)


func OrderChinaUmsPay(appId,openId string,acPay *isv.AcPayParams,order *shopping.ValueOrder)(rsp PayResponse,prepayId string, err error) {
	
	orderIdStr := strconv.FormatInt(order.OrderId, 10)
	var payInfoCache *PayInfoCache = new(PayInfoCache)
	cacheKey := "payorder_" + orderIdStr
	redisconn := redisgo.GetInstance()
	if cacheValue, e := redisconn.GetString(cacheKey); e == nil && len(cacheValue) > 5  {
		if err = json.Unmarshal([]byte(cacheValue), payInfoCache); err == nil {
			rsp.WxpayRsp = payInfoCache.SignInfo
	        rsp.OrderId = orderIdStr
			return
		}
	}
	mchId := acPay.MchId
	subMchId := acPay.SubMchId
	secreCode := acPay.Appkey
	//currency := acPay.MediaId
	msgSrc := acPay.PemCert
	attach := "ESHOP_ORDER_PAY"
	notifyUrl := beego.AppConfig.String("WXPayNotify") + "/public/chinaums/notify"
	var payment int = int(math.MultiTotalPriceInt(order.Payment, 100))
	var chinaUmsRsp chinaums.MiniPayRequest
	tmpRsp, e := chinaums.MicroAppPay(mchId,subMchId,msgSrc,secreCode,openId,orderIdStr,notifyUrl,attach,int(payment))
	if e != nil {
		beego.Debug(e.Error())
		err = e
		return 
	}
	err = json.Unmarshal(tmpRsp.MiniPayRequest, &chinaUmsRsp)
	if err != nil {
		beego.Debug(err.Error())
		return 
	}
	prepayId = strings.Replace(chinaUmsRsp.Package, "prepay_id=", "", -1)
	//更新订单支付方式编号
	order.PayWayCode = 16
	err = shopping.UpdateOrderPayWayCodeByOrderId(order.OrderId, order.PayWayCode)
	if err != nil {
		beego.Debug(err.Error())
	}
	payRsp := misc.WxResponse{}
	payRsp.AppId = chinaUmsRsp.AppId
	payRsp.NonceStr = chinaUmsRsp.NonceStr
	payRsp.Package = chinaUmsRsp.Package
	payRsp.SignType = chinaUmsRsp.SignType
	payRsp.TimeStamp = chinaUmsRsp.TimeStamp
	payRsp.PaySign = chinaUmsRsp.PaySign
	if tempJson, err := json.Marshal(payRsp); err == nil { 
	   rsp.WxpayRsp = string(tempJson)
	   rsp.OrderId = orderIdStr
	}
	payInfoCache.AppId = appId
	payInfoCache.MchId = acPay.MchId
	payInfoCache.AppKey = acPay.Appkey
	payInfoCache.OrderKind = "ESHOP_ORDER_PAY"
	payInfoCache.SignInfo = rsp.WxpayRsp
	if payInfoJSON, err := json.Marshal(payInfoCache); err == nil { 
		if _, err = redisconn.Set(cacheKey, string(payInfoJSON), 72*60*60); err != nil {  
			beego.Debug("SETEX", err.Error())
		}
	}
	return
}

func ChargeChinaUmsPay(appId,openId,outTradeNo string,acPay *isv.AcPayParams,amount float32)(rsp PayResponse,prepayId string,err error){
   
    mchId := acPay.MchId
	subMchId := acPay.SubMchId
	secreCode := acPay.Appkey
	//currency := acPay.MediaId
	msgSrc := acPay.PemCert
	
	attach := "MEMBER_CHARGE"
	notifyUrl := beego.AppConfig.String("WXPayNotify") + "/public/chinaums/notify"
	payment := math.MultiTotalPriceInt(amount, 100)
	tmpRsp, err := chinaums.MicroAppPay(mchId,subMchId,msgSrc,secreCode,openId,outTradeNo,notifyUrl,attach,int(payment))
	if err != nil {
		err = errors.Wrap(err, "ChinaUms MicroAppPay... ")
		beego.Debug(err.Error())
		return 
	}
	var chinaUmsRsp chinaums.MiniPayRequest
	err = json.Unmarshal(tmpRsp.MiniPayRequest, &chinaUmsRsp)
	if err != nil {
		beego.Debug(err.Error())
		return 
	}
	prepayId = strings.Replace(chinaUmsRsp.Package, "prepay_id=", "", -1)
	payRsp := misc.WxResponse{}
	payRsp.AppId = chinaUmsRsp.AppId
	payRsp.NonceStr = chinaUmsRsp.NonceStr
	payRsp.Package = chinaUmsRsp.Package
	payRsp.SignType = chinaUmsRsp.SignType
	payRsp.TimeStamp = chinaUmsRsp.TimeStamp
	payRsp.PaySign = chinaUmsRsp.PaySign
	if tempJson, err := json.Marshal(payRsp); err == nil {
		rsp.WxpayRsp = string(tempJson)
	}
	redisconn := redisgo.GetInstance()
	cacheKey := "payorder_" + outTradeNo
	var payInfoCache *PayInfoCache = new(PayInfoCache)
	payInfoCache.AppId = appId
	payInfoCache.MchId = acPay.MchId
	payInfoCache.AppKey = acPay.Appkey
	payInfoCache.OrderKind = "MEMBER_CHARGE"
	if payInfoJSON, err := json.Marshal(payInfoCache); err == nil { 
		if _, err = redisconn.Set(cacheKey, string(payInfoJSON), 72*60*60); err != nil {  
			beego.Debug("SETEX", err.Error())
		}
	}
	return
}

func CommonChinaUmsPay(appId,openId,orderId,body,kind string,acPay *isv.AcPayParams,amount float32)(rsp PayResponse,prepayId string, err error){
    mchId := acPay.MchId
	subMchId := acPay.SubMchId
	secreCode := acPay.Appkey
	//currency := acPay.MediaId
	msgSrc := acPay.PemCert
	attach := kind
	notifyUrl := beego.AppConfig.String("WXPayNotify") + "/public/chinaums/notify"
	payment := math.MultiTotalPriceInt(amount, 100)
	tmpRsp, err := chinaums.MicroAppPay(mchId,subMchId,msgSrc,secreCode,openId,orderId,notifyUrl,attach,int(payment))
	if err != nil {
		err = errors.Wrap(err, "ChinaUms MicroAppPay... ")
		beego.Debug(err.Error())
		return 
	}
	var chinaUmsRsp chinaums.MiniPayRequest
	err = json.Unmarshal(tmpRsp.MiniPayRequest, &chinaUmsRsp)
	if err != nil {
		beego.Debug(err.Error())
		return 
	}
	prepayId = strings.Replace(chinaUmsRsp.Package, "prepay_id=", "", -1)
	payRsp := misc.WxResponse{}
	payRsp.AppId = chinaUmsRsp.AppId
	payRsp.NonceStr = chinaUmsRsp.NonceStr
	payRsp.Package = chinaUmsRsp.Package
	payRsp.SignType = chinaUmsRsp.SignType
	payRsp.TimeStamp = chinaUmsRsp.TimeStamp
	payRsp.PaySign = chinaUmsRsp.PaySign
	if tempJson, err := json.Marshal(payRsp); err == nil { 
	   rsp.WxpayRsp = string(tempJson)
	}
	redisconn := redisgo.GetInstance()
	cacheKey := "payorder_" + orderId
	var payInfoCache *PayInfoCache = new(PayInfoCache)
	payInfoCache.AppId = appId
	payInfoCache.MchId = acPay.MchId
	payInfoCache.AppKey = acPay.Appkey
	payInfoCache.OrderKind = kind
	if payInfoJSON, err := json.Marshal(payInfoCache); err == nil { 
		if _, err = redisconn.Set(cacheKey, string(payInfoJSON), 72*60*60); err != nil {  
			beego.Debug("SETEX", err.Error())
		}
	}
	return
}
